package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.pojo.Location;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.LocationService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/location")
@RestController
public class LocationController {

    @Reference
    private LocationService locationService;

    /**
     * 查找所有的 体检中心
     *
     * @return
     */
    @RequestMapping("/findAllLocations")
    public Result findAll() {
        try {
            List<Location> locationList = locationService.findAll();
            return new Result(true, MessageConstant.GET_LOCATION_LIST_SUCCESS, locationList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_LOCATION_LIST_FAIL);
        }
    }

    /**
     * 复杂分页查询体检中心地址
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            return locationService.queryPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(null, null);
        }

    }


    /**
     * 添加地址
     *
     * @param location
     * @return
     */
    @RequestMapping("/addLocation")
    public Result addLocation(@RequestBody Location location) {
            return locationService.addLocation(location);
    }


    /**
     * 删除体检中心地址
     *
     * @param locationId
     * @return
     */
    @RequestMapping("/deleteById/{locationId}")
    public Result deleteById(@PathVariable("locationId") Integer locationId) {
        try {
            return  locationService.deleteById(locationId);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_LOCATION_FAIL);
        }
    }

}
